using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Reflection;
using Microsoft.Practices.Unity;

namespace $safeprojectname$.Unity
{
    /// <summary>
    /// Resolveur de dépendance
    /// </summary>
    public class UnityDependencyResolver : IDependencyResolver
    {
        IUnityContainer container;

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        public UnityDependencyResolver()
        {
            IUnityContainer container = (IoCFactory.Instance.CurrentContainer as IoCUnityContainer).Container;
            this.container = container;
        }

        /// <summary>
        /// Résolution d'un type de service
        /// </summary>
        /// <param name="serviceType">type de service</param>
        /// <returns>Instance du service</returns>
        public object GetService(Type serviceType)
        {
            try
            {
                return container.Resolve(serviceType);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Résolution d'un type de service
        /// </summary>
        /// <param name="serviceType">type de service</param>
        /// <returns>Liste d'instances du service</returns>
        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return container.ResolveAll(serviceType);
            }
            catch
            {
                return new List<object>();
            }
        }
    }
}
